using System;
using System.Collections.Generic;
using System.ComponentModel;
using BUS;
using DAO;
using DTO;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace QLVMB
{
    public partial class frmTraCuuChuyenBay : Form
    {
        //frmDatVe fdv = new frmDatVe();
        public frmTraCuuChuyenBay()
        {
            InitializeComponent();
        }
        public void XuatDuLieu(List<DTO.ChuyenBay> ds, List<DTO.ChiTietChuyenBay> dt, List<DTO.SanBay> tensb,List<DTO.MayBay> tenmb, int i, List<DTO.ChuyenBay> ds1)
        {
            ds1.Add(ds[i]);
            int m = dgTraCuu.Rows.Count - 2;
            dgTraCuu.Rows[m].Cells[0].Value = ds[i].MaChuyenBay;
            string s = ds[i].NgayBay.Day + "/" + ds[i].NgayBay.Month + "/" + ds[i].NgayBay.Year;
            dgTraCuu.Rows[m].Cells[1].Value = s;
            string d = ds[i].ThoiGian.Hour + ":" + ds[i].ThoiGian.Minute;
            dgTraCuu.Rows[m].Cells[2].Value = d;
            for (int j = 0; j < tensb.Count; j++)
            {
                if (ds[i].MaSanBayDi == tensb[j].MaSanBay)
                    dgTraCuu.Rows[m].Cells[3].Value = tensb[j].TenSanBay;
            }
            for (int j = 0; j < tensb.Count; j++)
            {
                if (ds[i].MaSanBayDen == tensb[j].MaSanBay)
                    dgTraCuu.Rows[m].Cells[4].Value = tensb[j].TenSanBay;
            }

            for (int k = 0; k < dt.Count; k++)
            {
                if (ds[i].MaChuyenBay == dt[k].MaChuyenBay)
                {

                    for (int j = 0; j < tensb.Count; j++)
                    {
                        if (dt[k].SanBayTrungGian == tensb[j].MaSanBay)
                            dgTraCuu.Rows[m].Cells[5].Value = tensb[j].TenSanBay;
                    }
                }

            }
            dgTraCuu.Rows[m].Cells[6].Value = ds[i].SoGheThuongGia;
            dgTraCuu.Rows[m].Cells[7].Value = ds[i].SoGhePhoThong;
            for (int j = 0; j < tenmb.Count; j++)
            {
                if (ds[i].MaLoaiMayBay == tenmb[j].MaLoaiMayBay)
                    dgTraCuu.Rows[m].Cells[8].Value = tenmb[j].TenLoaiMayBay;
            }

        }
        private void frmTraCuuChuyenBay_Load(object sender, EventArgs e)
        {
            List<DTO.ChuyenBay> ds = new List<DTO.ChuyenBay>();
            List<DTO.ChiTietChuyenBay> dt = new List<DTO.ChiTietChuyenBay>();
            List<DTO.SanBay> tensb = new List<DTO.SanBay>();
            List<DTO.SanBay> tensb2 = new List<DTO.SanBay>();
            List<DTO.ChuyenBay> ds1 = new List<DTO.ChuyenBay>();
            List<DTO.MayBay> tenmb = new List<DTO.MayBay>();
            //Them du lieu vao 2 Combobox
            BUS.SanBayBUSResult rs = BUS.SanBayBUS.ChonHet(tensb);
            BUS.SanBayBUSResult rs2 = BUS.SanBayBUS.ChonHet(tensb2);
            DTO.SanBay sb = new DTO.SanBay();
            sb.MaSanBay = 0;
            sb.TenSanBay = "";
            tensb.Insert(0, sb);
            tensb2.Insert(0, sb);
            cbSanBayDi.DataSource = tensb;
            cbSanBayDi.DisplayMember = "TenSanBay";
            cbSanBayDi.ValueMember = "MaSanBay";
            cbSanBayDen.DataSource = tensb2;
            cbSanBayDen.DisplayMember = "TenSanBay";
            cbSanBayDen.ValueMember = "MaSanBay";
            //Hoan tat them.

            //Them du lieu vao 3 comboBox Ngay,Thang,Nam
            for (int i = 1; i <= 31; i++)
            {

                cbNgay.Items.Add(i);
            }
            for (int i = 1; i <= 12; i++)
            {
                cbThang.Items.Add(i);
            }
            for (int i = 2000; i <= 2011; i++)
            {
                cbNam.Items.Add(i);
            }
            string tngay = "";
            string tthang = "";
            string tnam = "";
            cbNgay.Items.Insert(0, tngay);
            cbThang.Items.Insert(0, tthang);
            cbNam.Items.Insert(0, tnam);
            //cbNgay.SelectedItem = 0;
            //cbThang.SelectedItem = 0;
            //cbNam.SelectedItem = 0;
            //Hoan tat them.
            if (BUS.ChuyenBayBUS.ChonHetChuyenBay(ds) == BUS.ChuyenBayBUSResult.OK && BUS.SanBayBUS.ChonHet(tensb) == BUS.SanBayBUSResult.OK && BUS.ChiTietChuyenBayBUS.ChonHetCTChuyenBay(dt) == BUS.ChiTietChuyenBayBUSResult.OK && BUS.MayBayBUS.ChonHetMayBay(tenmb) == BUS.MayBayBUSResult.OK)
            {
                for (int i = 0; i < ds.Count; i++)
                {

                    dgTraCuu.Rows.Insert(dgTraCuu.Rows.Count - 1);
                    XuatDuLieu(ds, dt, tensb,tenmb, i, ds1);


                }
            }
        }
        //Tra cuu theo MaChuyenBay
        public int TraCuuMaChuyenBay(List<DTO.ChuyenBay> ds, List<DTO.ChiTietChuyenBay> dt, List<DTO.SanBay> tensb, List<DTO.MayBay> tenmb, List<DTO.ChuyenBay> ds1, string mcb)
        {
            int temp = 0;
            for (int i = 0; i < ds.Count; i++)
            {
                if (mcb != "")
                {
                    if (mcb == ds[i].MaChuyenBay)
                    {
                        temp = 1;
                        dgTraCuu.Rows.Insert(dgTraCuu.Rows.Count - 1);
                        XuatDuLieu(ds, dt, tensb, tenmb, i, ds1);

                    }
                }

            }
            return temp;
        }

        //Tra cuu theo SanBayDi
        public int TraCuuSanBayDi(List<DTO.ChuyenBay> ds, List<DTO.ChiTietChuyenBay> dt, List<DTO.SanBay> tensb, List<DTO.MayBay> tenmb, List<DTO.ChuyenBay> ds1, int sbdi)
        {
            int temp = 0;
            for (int i = 0; i < ds.Count; i++)
            {
                if (sbdi != 0)
                {
                    if (sbdi == ds[i].MaSanBayDi)
                    {
                        temp = 1;
                        dgTraCuu.Rows.Insert(dgTraCuu.Rows.Count - 1);
                        XuatDuLieu(ds, dt, tensb, tenmb, i, ds1);
                    }
                }
            }
            return temp;
        }

        //Tra cuu theo SanBayDen
        public int TraCuuSanBayDen(List<DTO.ChuyenBay> ds, List<DTO.ChiTietChuyenBay> dt, List<DTO.SanBay> tensb, List<DTO.MayBay> tenmb, List<DTO.ChuyenBay> ds1, int sbden)
        {
            int temp = 0;
            for (int i = 0; i < ds.Count; i++)
            {
                if (sbden != 0)
                {
                    if (sbden == ds[i].MaSanBayDen)
                    {
                        temp = 1;
                        dgTraCuu.Rows.Insert(dgTraCuu.Rows.Count - 1);
                        XuatDuLieu(ds, dt, tensb, tenmb, i, ds1);
                    }
                }
            }
            return temp;
        }

        //Tra cuu theo ngaythangnam
        public int TraCuuTheoNgay(List<DTO.ChuyenBay> ds, List<DTO.ChiTietChuyenBay> dt, List<DTO.SanBay> tensb, List<DTO.MayBay> tenmb, List<DTO.ChuyenBay> ds1, int ngay, int thang, int nam)
        {
            int temp = 0;
            for (int i = 0; i < ds.Count; i++)
            {
                if (ngay != -1 && thang != -1 && nam != -1)
                {
                    if (ngay == ds[i].NgayBay.Day && thang == ds[i].NgayBay.Month && nam + 1999 == ds[i].NgayBay.Year)
                    {
                        temp = 1;
                        dgTraCuu.Rows.Insert(dgTraCuu.Rows.Count - 1);
                        XuatDuLieu(ds, dt, tensb, tenmb, i, ds1);
                    }
                }

            }
            return temp;
        }

        private void btTraCuu_Click(object sender, EventArgs e)
        {
            dgTraCuu.Rows.Clear();
            List<DTO.ChuyenBay> ds = new List<DTO.ChuyenBay>();
            List<DTO.ChiTietChuyenBay> dt = new List<DTO.ChiTietChuyenBay>();
            List<DTO.SanBay> tensb = new List<DTO.SanBay>();
            List<DTO.ChuyenBay> ds1 = new List<DTO.ChuyenBay>();
            List<DTO.MayBay> tenmb = new List<DTO.MayBay>();
            
            int ngay = int.Parse(cbNgay.SelectedIndex.ToString());
            int thang = int.Parse(cbThang.SelectedIndex.ToString());
            int nam = int.Parse(cbNam.SelectedIndex.ToString());
            int sbden = int.Parse(cbSanBayDen.SelectedValue.ToString());
            int sbdi = int.Parse(cbSanBayDi.SelectedValue.ToString());
            string mcb = txtMaChuyenBay.Text;
            if (BUS.ChuyenBayBUS.ChonHetChuyenBay(ds) == BUS.ChuyenBayBUSResult.OK && BUS.SanBayBUS.ChonHet(tensb) == BUS.SanBayBUSResult.OK && BUS.ChiTietChuyenBayBUS.ChonHetCTChuyenBay(dt) == BUS.ChiTietChuyenBayBUSResult.OK)
            {

                int temp;
                if ((ngay != -1 && thang == -1 && nam == -1) || (ngay == -1 && thang != -1 && nam == -1) || (ngay != -1 && thang != -1 && nam == -1) || (ngay == -1 && thang == -1 && nam != -1) || (ngay != -1 && thang == -1 && nam != -1) || (ngay == -1 && thang != -1 && nam != -1))
                {
                    MessageBox.Show("Ban phai chon day du ngay thang nam");
                }
                // MaChuyenBay
                if (sbdi == 0 && sbden == 0 && ngay == -1 && thang == -1 && nam == -1)
                {
                    temp = TraCuuMaChuyenBay(ds, dt, tensb, tenmb, ds1, mcb);
                    ds1.Clear();
                    if (temp == 0)
                    {
                        MessageBox.Show("Khong tim thay thong tin trung voi thong tin ban da nhap. Vui long nhap lai ,^^!.");
                    }
                }
                //Ngay
                if (mcb == "" && sbdi == 0 && sbden == 0 && ngay != -1 && thang!= -1 && nam != -1)
                {

                    temp = TraCuuTheoNgay(ds, dt, tensb, tenmb, ds1, ngay, thang, nam);
                    ds1.Clear();
                    if (temp == 0)
                    {
                        MessageBox.Show("Khong tim thay thong tin trung voi thong tin ban da nhap. Vui long nhap lai ,^^!.");
                    }
                }
                //SanBayDi
                if (mcb == "" && sbden == 0 && ngay == -1 && thang == -1 && nam == -1)
                {
                    temp = TraCuuSanBayDi(ds, dt, tensb, tenmb, ds1, sbdi);
                    ds1.Clear();
                    if (temp == 0)
                    {
                        MessageBox.Show("Khong tim thay thong tin trung voi thong tin ban da nhap. Vui long nhap lai ,^^!.");
                    }
                }
                //SanBayDen
                if (mcb == "" && sbdi == 0 && ngay == -1 && thang == -1 && nam == -1)
                {
                    temp = TraCuuSanBayDen(ds, dt, tensb, tenmb, ds1, sbden);
                    ds1.Clear();
                    if (temp == 0)
                    {
                        MessageBox.Show("Khong tim thay thong tin trung voi thong tin ban da nhap. Vui long nhap lai ,^^!.");
                    }
                }
                //SanBayDi,SanBayDen
                if(mcb=="" && ngay == -1 && thang == -1 && nam == -1&&sbden!=0 &&sbdi!=0)
                {
                    temp = TraCuuSanBayDi(ds, dt, tensb, tenmb, ds1, sbdi);
                    dgTraCuu.Rows.Clear();
                    List<DTO.ChuyenBay> ds2 = new List<DTO.ChuyenBay>();
                    temp = TraCuuSanBayDen(ds1, dt, tensb, tenmb, ds2, sbden);
                    if (temp == 0)
                    {
                            MessageBox.Show("Khong tim thay thong tin trung voi thong tin ban da nhap. Vui long nhap lai ,^^!.");
                    }
                }
                //Ngay,SanBayDen
                if (mcb == "" && ngay != -1 && thang != -1 && nam != -1 && sbden != 0 && sbdi == 0)
                {
                    temp = TraCuuSanBayDen(ds, dt, tensb, tenmb, ds1, sbden);
                    dgTraCuu.Rows.Clear();
                    List<DTO.ChuyenBay> ds2 = new List<DTO.ChuyenBay>();
                    temp = TraCuuTheoNgay(ds1, dt, tensb, tenmb, ds2, ngay, thang, nam);
                    if (temp == 0)
                    {
                        MessageBox.Show("Khong tim thay thong tin trung voi thong tin ban da nhap. Vui long nhap lai ,^^!.");
                    }
                }
                //Ngay,SanBayDi
                if (mcb == "" && ngay != -1 && thang != -1 && nam != -1 && sbden == 0 && sbdi != 0)
                {
                    temp = TraCuuSanBayDi(ds, dt, tensb, tenmb, ds1, sbdi);
                    dgTraCuu.Rows.Clear();
                    List<DTO.ChuyenBay> ds2 = new List<DTO.ChuyenBay>();
                    temp = TraCuuTheoNgay(ds1, dt, tensb, tenmb, ds2, ngay, thang, nam);
                    if (temp == 0)
                    {
                        MessageBox.Show("Khong tim thay thong tin trung voi thong tin ban da nhap. Vui long nhap lai ,^^!.");
                    }
                }
                //Machuyenbay,Sanbayden
                if (mcb != "" && ngay == -1 && thang == -1 && nam == -1 && sbden != 0 && sbdi == 0)
                {
                    temp = TraCuuSanBayDen(ds, dt, tensb, tenmb, ds1, sbden);
                    dgTraCuu.Rows.Clear();
                    List<DTO.ChuyenBay> ds2 = new List<DTO.ChuyenBay>();
                    temp = TraCuuMaChuyenBay(ds1, dt, tensb, tenmb, ds2, mcb);
                    if (temp == 0)
                    {
                        MessageBox.Show("Khong tim thay thong tin trung voi thong tin ban da nhap. Vui long nhap lai ,^^!.");
                    }
                }
                //Machuyenbay,SanBayDi
                if (mcb != "" && ngay == -1 && thang == -1 && nam == -1 && sbden == 0 && sbdi != 0)
                {
                    temp = TraCuuSanBayDi(ds, dt, tensb, tenmb, ds1, sbdi);
                    dgTraCuu.Rows.Clear();
                    List<DTO.ChuyenBay> ds2 = new List<DTO.ChuyenBay>();
                    temp = TraCuuMaChuyenBay(ds1, dt, tensb, tenmb, ds2, mcb);
                    if (temp == 0)
                    {
                        MessageBox.Show("Khong tim thay thong tin trung voi thong tin ban da nhap. Vui long nhap lai ,^^!.");
                    }
                }
                //Machuyenbay,Ngay
                if (mcb != "" && ngay != -1 && thang != -1 && nam != -1 && sbden == 0 && sbdi == 0)
                {
                    temp = TraCuuTheoNgay(ds, dt, tensb, tenmb, ds1, ngay, thang, nam);
                    dgTraCuu.Rows.Clear();
                    List<DTO.ChuyenBay> ds2 = new List<DTO.ChuyenBay>();
                    temp = TraCuuMaChuyenBay(ds1, dt, tensb, tenmb, ds2, mcb);
                    if (temp == 0)
                    {
                        MessageBox.Show("Khong tim thay thong tin trung voi thong tin ban da nhap. Vui long nhap lai ,^^!.");
                    }
                }
                //Ngay,SanBayDi,SanBayDen
                if (mcb == "" && ngay != -1 && thang != -1 && nam != -1 && sbden != 0 && sbdi != 0)
                {
                    temp = TraCuuTheoNgay(ds, dt, tensb, tenmb, ds1, ngay, thang, nam);
                    dgTraCuu.Rows.Clear();
                    List<DTO.ChuyenBay> ds2 = new List<DTO.ChuyenBay>();
                    temp = TraCuuSanBayDi(ds1, dt, tensb, tenmb, ds2, sbdi);
                    dgTraCuu.Rows.Clear();
                    List<DTO.ChuyenBay> ds3 = new List<DTO.ChuyenBay>();
                    temp = TraCuuSanBayDen(ds2, dt, tensb, tenmb, ds3, sbden);
                    if (temp == 0)
                    {
                        MessageBox.Show("Khong tim thay thong tin trung voi thong tin ban da nhap. Vui long nhap lai ,^^!.");
                    }
                }
                //MaChuyenBay,Ngay,SanBayDen
                if (mcb != "" && ngay != -1 && thang != -1 && nam != -1 && sbden != 0 && sbdi == 0)
                {
                    temp = TraCuuTheoNgay(ds, dt, tensb, tenmb, ds1, ngay, thang, nam);
                    dgTraCuu.Rows.Clear();
                    List<DTO.ChuyenBay> ds2 = new List<DTO.ChuyenBay>();
                    temp = TraCuuMaChuyenBay(ds1, dt, tensb, tenmb, ds2, mcb);
                    dgTraCuu.Rows.Clear();
                    List<DTO.ChuyenBay> ds3 = new List<DTO.ChuyenBay>();
                    temp = TraCuuSanBayDen(ds2, dt, tensb, tenmb, ds3, sbden);
                    if (temp == 0)
                    {
                        MessageBox.Show("Khong tim thay thong tin trung voi thong tin ban da nhap. Vui long nhap lai ,^^!.");
                    }
                }
                //Machuyenbay,ngaybay,sanbaydi
                if (mcb != "" && ngay != -1 && thang != -1 && nam != -1 && sbden == 0 && sbdi != 0)
                {
                    temp = TraCuuTheoNgay(ds, dt, tensb, tenmb, ds1, ngay, thang, nam);
                    dgTraCuu.Rows.Clear();
                    List<DTO.ChuyenBay> ds2 = new List<DTO.ChuyenBay>();
                    temp = TraCuuMaChuyenBay(ds1, dt, tensb, tenmb, ds2, mcb);
                    dgTraCuu.Rows.Clear();
                    List<DTO.ChuyenBay> ds3 = new List<DTO.ChuyenBay>();
                    temp = TraCuuSanBayDi(ds2, dt, tensb, tenmb, ds3, sbdi);
                    if (temp == 0)
                    {
                        MessageBox.Show("Khong tim thay thong tin trung voi thong tin ban da nhap. Vui long nhap lai ,^^!.");
                    }
                }
                //THEO 4 TIEU CHI
                if (mcb != "" && ngay != -1 && thang != -1 && nam != -1 && sbden != 0 && sbdi != 0)
                {
                    temp = TraCuuTheoNgay(ds, dt, tensb, tenmb, ds1, ngay, thang, nam);
                    dgTraCuu.Rows.Clear();
                    List<DTO.ChuyenBay> ds2 = new List<DTO.ChuyenBay>();
                    temp = TraCuuMaChuyenBay(ds1, dt, tensb, tenmb, ds2, mcb);
                    dgTraCuu.Rows.Clear();
                    List<DTO.ChuyenBay> ds3 = new List<DTO.ChuyenBay>();
                    temp = TraCuuSanBayDi(ds2, dt, tensb, tenmb, ds3, sbdi);
                    dgTraCuu.Rows.Clear();
                    List<DTO.ChuyenBay> ds4 = new List<DTO.ChuyenBay>();
                    temp = TraCuuSanBayDen(ds3, dt, tensb, tenmb, ds4, sbden);
                    if (temp == 0)
                    {
                        MessageBox.Show("Khong tim thay thong tin trung voi thong tin ban da nhap. Vui long nhap lai ,^^!.");
                    }
                }
            }
        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            
            //frmDatVe dv;
           // Form a = frmMainQL.ActiveForm.Parent();
           // a.ParentForm.Show();
            //frmDatVe dv = new frmDatVe();
            //this.Close();
            //this.Close();

            //frmMainQL a = fr.ActiveForm;
            //a.Show();
            
            
            //this.Close();
            
            Thread thread = new Thread(new ThreadStart(MoFormDatVe));
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            //ActiveForm.Close();
            System.Windows.Forms.Application.Exit();  
            //ActiveForm.Visible = false;
            //ActiveForm.IsMdiContainer = false;
            //ActiveForm.Close();
            
            //ActiveForm.Visible = false;
            //a.panel1.Controls.Clear();
            //dv.MdiParent = a;
            //a.panel1.Controls.Add(dv);
            //dv.Show();
            
        }
        void MoFormDatVe()
        {
            List<DTO.SanBay> lsb = new List<DTO.SanBay>();
            List<DTO.MayBay> lmb = new List<DTO.MayBay>();
            DTO.ChuyenBay temp = new DTO.ChuyenBay();
            temp.MaChuyenBay = dgTraCuu.CurrentRow.Cells[0].Value.ToString();
            temp.NgayBay = DateTime.Parse(dgTraCuu.CurrentRow.Cells[1].Value.ToString());
            temp.ThoiGian = DateTime.Parse(dgTraCuu.CurrentRow.Cells[2].Value.ToString());
            temp.SoGheThuongGia = int.Parse(dgTraCuu.CurrentRow.Cells[6].Value.ToString());
            temp.SoGhePhoThong = int.Parse(dgTraCuu.CurrentRow.Cells[7].Value.ToString());

            string sbdi = dgTraCuu.CurrentRow.Cells[3].Value.ToString();
            string sbden = dgTraCuu.CurrentRow.Cells[4].Value.ToString() ;
            string mb = dgTraCuu.CurrentRow.Cells[8].Value.ToString();

            if (BUS.SanBayBUS.ChonHet(lsb) == BUS.SanBayBUSResult.OK)
            {
                for (int i = 0; i < lsb.Count; i++)
                {
                    if ( sbdi== lsb[i].TenSanBay)
                        temp.MaSanBayDi = lsb[i].MaSanBay;
                    if (sbden == lsb[i].TenSanBay)
                        temp.MaSanBayDen = lsb[i].MaSanBay;
                }
            }
            if (BUS.MayBayBUS.ChonHetMayBay(lmb) == BUS.MayBayBUSResult.OK)
            {
                for (int i = 0; i < lmb.Count ; i++)
                {
                    if (mb == lmb[i].TenLoaiMayBay)
                        temp.MaLoaiMayBay = lmb[i].MaLoaiMayBay;
                }
            }
            frmMainQL a = new frmMainQL();
            frmSuaChuyenBay dv = new frmSuaChuyenBay();
            dv.cb = temp;
            a.Show();
            a.panel1.Controls.Clear();
            dv.MdiParent = a;
            a.panel1.Controls.Add(dv);
            
           
            //dv.Show();
            System.Windows.Forms.Application.Run(dv);
            
        } 
       

        
      
        

    }
}